//
//  YKCategoryCell.m
//  HMPersonalDoctor
//
//  Created by 叶文凯 on 16/9/7.
//  Copyright © 2016年 geduo. All rights reserved.
//

#import "YKCategoryCell.h"
#import "NSAttributedString+CZAdditon.h"
#import "CCKYTools.h"
#import "AFNetworking.h"
#import "YKWebViewController.h"


@interface YKCategoryCell ()




@end

@implementation YKCategoryCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
        
    }
    
    return self;
}

- (void)setupUI
{
//    图文混排
//    self.contentView.backgroundColor = [UIColor redColor];
    
    
    [self addButtonWithTitle:@"男科" andImageName:@"c1" andTag:1];
    [self addButtonWithTitle:@"儿科" andImageName:@"c2" andTag:2];
    [self addButtonWithTitle:@"外科" andImageName:@"c3" andTag:3];
    [self addButtonWithTitle:@"妇科" andImageName:@"c4"andTag:4];
    [self addButtonWithTitle:@"骨科" andImageName:@"c5" andTag:5];
    [self addButtonWithTitle:@"心血管" andImageName:@"c6" andTag:6];
    [self addButtonWithTitle:@"肿瘤科" andImageName:@"c7" andTag:7];
    [self addButtonWithTitle:@"更多" andImageName:@"c8" andTag:8];
}


- (void)addButtonWithTitle:(NSString *)title andImageName:(NSString *)imageName andTag:(NSInteger)tag
{
    UIButton *button = [[UIButton alloc] init];
    
    NSAttributedString *str = [NSAttributedString cz_imageTextWithImage:[UIImage imageNamed:imageName] imageWH:40 title:title fontSize:15 titleColor:[UIColor blackColor] spacing:8];
    
    [button setAttributedTitle:str forState:UIControlStateNormal];
    
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    [button sizeToFit];
    button.titleLabel.numberOfLines = 0;
    
    [self.contentView addSubview:button];
    
    button.tag = tag;
    
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
}

- (void)layoutSubviews
{
    
    [super layoutSubviews];
    
//    60
    CGFloat margin =(self.contentView.bounds.size.width - self.contentView.subviews[0].bounds.size.width*4)/5;
    
    [self.contentView.subviews enumerateObjectsUsingBlock:^(__kindof UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        CGRect frame = obj.frame;
        
        CGFloat width = 45;
        CGFloat hight = 79.5;
//  测试数据：
//        (origin = (x = 60, y = 0), size = (width = 45, height = 79.5))
        
        if (idx < 4) {
            
            frame = CGRectMake(idx*(width + margin) + margin, 0, width, hight);
            obj.frame = frame;

        }
        //       (origin = (x = 270, y = 0), size = (width = 45, height = 79.5))

        if (idx > 3) {
            
            frame = CGRectMake((idx%4)*(width + margin) + margin, self.contentView.frame.origin.y + hight, width, hight);
            obj.frame = frame;

        }

    }];

}

- (void)buttonAction:(UIButton *)button
{
    
    if (button.tag != 8) {
        
        NSString *title = button.titleLabel.text;
        
        if ([self.delgate respondsToSelector:@selector(pushCategoryController:)]) {
            
            [self.delgate pushCategoryController:title];
        }
        return;
    }
    
  
    if ([self.delgate respondsToSelector:@selector(loadWebViewController: )]) {
        
        [self.delgate loadWebViewController:button.tag];
        
    }

}

@end
